'''
Created on Jan 29, 2012

@author: Michal Golinski, Kamil Litwiniuk
'''

from SimPy.Simulation import *
from Host import *
from Router import *
from SimPy.SimPlot import *


if __name__ == '__main__':
    pass

DEFAULT_FLOW_PATH = "H1-R1-end"
MAX_PACKET_SIZE = 6000
PACKETS_PER_SECOND = 20
DEFAULT_TYPE_OF_SERVICE = 0
OUTPUT_SPEED = 1000000
SIMULATION_TIME= 100
DEFAULT_PACKET_RANDOMNESS = "random"

def testScenario(schedulerCode, packetType):
    
    def chooseScheduler(schedulerCode):
        if schedulerCode == "FCFS":
            scheduler = FcfsScheduler()
        elif schedulerCode == "RR":
            scheduler = RrScheduler()
        elif schedulerCode == "DRR":
            scheduler = DrrScheduler()
        else:
            raise Exception('Wrong scheduler code' + schedulerCode)
        return scheduler
    
    initialize()
    
    print "Test = ", schedulerCode
    print "Dwa hosty i pojedynczy router. Summarycznie 40 strumieni."
    
    router1 = Router("R1")
    scheduler = chooseScheduler(schedulerCode) 
    outputPort1 = OutputPort(router1, "end", scheduler)
    router1.outputPorts.append(outputPort1)
    router1.activatePorts(finishTime = SIMULATION_TIME)
    
    host1 = Host(1, 19, router1, DEFAULT_FLOW_PATH, MAX_PACKET_SIZE, PACKETS_PER_SECOND, DEFAULT_TYPE_OF_SERVICE, packetType) 
    flow = Flow(host1, 19, DEFAULT_FLOW_PATH, MAX_PACKET_SIZE, PACKETS_PER_SECOND*2, DEFAULT_TYPE_OF_SERVICE, packetType)
    host1.flows.append(flow)
    activate(flow, flow.generateMessages(SIMULATION_TIME))
    
    host2 = Host(2, 19, router1, DEFAULT_FLOW_PATH, MAX_PACKET_SIZE, PACKETS_PER_SECOND, DEFAULT_TYPE_OF_SERVICE, packetType) 
    flow = Flow(host2, 19, DEFAULT_FLOW_PATH, MAX_PACKET_SIZE, PACKETS_PER_SECOND*3, DEFAULT_TYPE_OF_SERVICE, packetType)
    host1.flows.append(flow)
    activate(flow, flow.generateMessages(SIMULATION_TIME))
    
    activate(router1, router1.receiveMessage(finishTime = SIMULATION_TIME))
    activate(host1, host1.sendPackets(finishTime = SIMULATION_TIME))
    activate(host2, host2.sendPackets(finishTime = SIMULATION_TIME))
    
    simulate(until=SIMULATION_TIME)
    
    print "Koniec testu", scheduler.name
    
    #############################################
    
    plotData = []
    
    i = 1
    for monitor in router1.outputPorts[0].outputMonitor.monitors.values():
        plotData.append([i, monitor.total()/SIMULATION_TIME])
        i = i+1

    return plotData

plotData1 = testScenario("FCFS", DEFAULT_PACKET_RANDOMNESS)
plotData2 = testScenario("RR", DEFAULT_PACKET_RANDOMNESS)
plotData3 = testScenario("DRR", DEFAULT_PACKET_RANDOMNESS)


    
plt=SimPlot()
plt.root.title("Advanced API example")
line1=plt.makeLine(plotData1, color='red', width=2)
line2=plt.makeLine(plotData2, color='green', width=2) 
line3=plt.makeLine(plotData3, color='blue', width=2) 

obj=plt.makeGraphObjects([line1, line2, line3])  
#obj=plt.makeGraphObjects([line1])  

frame=Frame(plt.root)
graph=plt.makeGraphBase(frame,600,600, title="Test 1", xtitle="Numer strumienia", ytitle="Przepustowosc [bits/s]")
graph.pack(expand = True)
graph.draw(obj)
frame.pack(expand = True) 
#graph.postscr()
plt.mainloop()